package com.mybatis.lo.utils;

import java.lang.reflect.Field;

/**
 * 反射工具
 * @author zhangbh
 * 2023-06-20 09:11
 **/
public class ReflectionUtils {

    public static <T> T newInstance(Class<?> resultType) throws InstantiationException, IllegalAccessException {
        //todo 当前按简单来
        return (T) resultType.newInstance();
    }

    public static Field getField(Object data,String fieldName) throws NoSuchFieldException {
        Class<?> clazz = data.getClass();
        Field field = clazz.getDeclaredField(fieldName);
        field.setAccessible(true);
        return field;
    }

    public static <T> void setFieldValue(T data, String fieldName, Object fieldValue) throws NoSuchFieldException, IllegalAccessException {
        Class<?> clazz = data.getClass();
        Field field = clazz.getDeclaredField(fieldName);
        field.setAccessible(true);
        field.set(data, fieldValue);
    }
}
